$(document).ready(function(e) {
	//创建分页层
	createPageTab();
	
	getCompanyList(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var companyList = null;
function getCompanyList(pageNow){
	var tmp2=root+"/admin/company!findCompanyByP";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc},
		dataType:"json",
		success: function(json){
			totalPageNum = json.totalPageNum;
			companyList = json.pageData;
			//创建表格头信息
			createListTab();
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].content+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1"><a href="'+root+'/admin/company!goSuperMarket?company.id='+json.pageData[i].id+'&company.name='+json.pageData[i].name+'">旗下门店</a></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdateCompany('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="deleteCompany('+json.pageData[i].id+','+json.curPageNum+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){getCompanyList(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){getCompanyList((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){getCompanyList((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){getCompanyList(json.totalPageNum);});
		}
	})
}

function goSuperMarket(companyName){
	//window.location.href = root+'/admin/company!goSuperMarket?company.name='+companyName;
	var tmp2=root+"/admin/company!goSuperMarket";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"company.name":companyName},
		dataType:"json",
		success: function(json){
			
		}
	})
}

function addCompany(){
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAddCompany(){
	$("#addCompanyForm")[0].reset();
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function saveCompany(){
	if($("#companyName").val()!="" && $("#companyContent").val()!=""){
		$("#addCompanyForm").submit();
	}else{
		alert("系统名跟系统简介不能为空！");
	}
}

function showUpdateCompany(pageNow, index){
	$("#backGroud").show();
	$("#updateDiv").show();
	$("#companyId_u").val(companyList[index].id);
	$("#companyName_u").val(companyList[index].name);
	$("#companyContent_u").val(companyList[index].content);
	$("#updateCompanyButton").unbind("click");
	$("#updateCompanyButton").bind("click",function(){updateCompany(pageNow);});
}

function closeUpdateCompany(){
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function updateCompany(pageNow){
	var tmp2=root+"/admin/company!updateCompany";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"company.id":$("#companyId_u").val(),"company.name":$("#companyName_u").val(), "company.content": $("#companyContent_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdateCompany();
			if(json.code=="y"){
				getCompanyList(pageNow);
			}else{
				alert("修改失败，请稍后重试！");
			}
		}
	})
}

/**
 * 删除系统
 * @param {Object} companyID
 * @param {Object} pageNow
 */
function deleteCompany(companyID, pageNow){
	var ids = "";
	if(companyID==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个公司！");
			return;
		}
	}
	var tmp2=root+"/admin/company!deleteCompany";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"company.id":companyID, "ids":ids, "page.curPageNum":pageNow, "isDesc": isDesc},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			getCompanyList(json.curPageNum);
		}
	})
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		getCompanyList(p);
	}
}
